<template>
  <nav class="navbar">
    <div class="nav-container">
      <!-- Logo -->
      <div class="nav-brand">
        <router-link to="/posts" class="brand-link">
          <el-icon class="brand-icon"><Edit /></el-icon>
          <span class="brand-text">博客系统</span>
        </router-link>
      </div>

      <!-- 导航菜单 -->
      <div class="nav-menu" :class="{ 'nav-menu-open': menuOpen }">
        <router-link to="/posts" class="nav-link" @click="closeMenu">逛一逛</router-link>
        <router-link to="/write" class="nav-link" @click="closeMenu">写博客</router-link>
        <router-link v-if="userStore.isLoggedIn" to="/my-posts" class="nav-link" @click="closeMenu">我的博客</router-link>
        
        <!-- 用户菜单 -->
        <div class="user-menu" v-if="userStore.isLoggedIn">
          <el-dropdown @command="handleUserCommand">
            <div class="user-info">
              <el-avatar :src="userStore.user?.avatar_url" :size="32">
                {{ userStore.user?.username?.charAt(0).toUpperCase() }}
              </el-avatar>
              <span class="username">{{ userStore.user?.username }}</span>
              <el-icon><ArrowDown /></el-icon>
            </div>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="drafts">
                  <el-icon><Document /></el-icon>
                  草稿箱
                </el-dropdown-item>
                <el-dropdown-item command="profile">
                  <el-icon><User /></el-icon>
                  个人中心
                </el-dropdown-item>
                <el-dropdown-item command="logout" divided>
                  <el-icon><SwitchButton /></el-icon>
                  退出登录
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>

        <!-- 未登录用户菜单 -->
        <div class="auth-menu" v-else>
          <el-button @click="$router.push('/login')" type="primary" plain>
            登录
          </el-button>
          <el-button @click="$router.push('/register')">
            注册
          </el-button>
        </div>
      </div>

      <!-- 移动端菜单按钮 -->
      <div class="nav-toggle" @click="toggleMenu">
        <el-icon><Menu /></el-icon>
      </div>
    </div>
  </nav>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { ElMessage } from 'element-plus'

const router = useRouter()
const userStore = useUserStore()
const menuOpen = ref(false)

const toggleMenu = () => {
  menuOpen.value = !menuOpen.value
}

const closeMenu = () => {
  menuOpen.value = false
}

const handleUserCommand = (command: string) => {
  switch (command) {
    case 'drafts':
      router.push('/drafts')
      break
    case 'profile':
      router.push('/profile')
      break
    case 'logout':
      userStore.logout()
      ElMessage.success('已退出登录')
      router.push('/')
      break
  }
  closeMenu()
}
</script>

<style scoped>
.navbar {
  background: white;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 1000;
}

.nav-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 64px;
}

.nav-brand {
  display: flex;
  align-items: center;
}

.brand-link {
  display: flex;
  align-items: center;
  text-decoration: none;
  color: #333;
  font-weight: 700;
  font-size: 1.25rem;
}

.brand-icon {
  margin-right: 8px;
  font-size: 1.5rem;
  color: #409eff;
}

.brand-text {
  color: #333;
}

.nav-menu {
  display: flex;
  align-items: center;
  gap: 2rem;
}

.nav-link {
  text-decoration: none;
  color: #666;
  font-weight: 500;
  transition: color 0.3s;
}

.nav-link:hover,
.nav-link.router-link-active {
  color: #409eff;
}

.user-menu {
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  padding: 8px 12px;
  border-radius: 6px;
  transition: background-color 0.3s;
}

.user-info:hover {
  background-color: #f5f7fa;
}

.username {
  font-weight: 500;
  color: #333;
}

.auth-menu {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.nav-toggle {
  display: none;
  cursor: pointer;
  font-size: 1.5rem;
  color: #333;
}

@media (max-width: 768px) {
  .nav-menu {
    position: fixed;
    top: 64px;
    left: 0;
    right: 0;
    background: white;
    flex-direction: column;
    padding: 2rem;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    transform: translateY(-100%);
    opacity: 0;
    visibility: hidden;
    transition: all 0.3s;
  }

  .nav-menu-open {
    transform: translateY(0);
    opacity: 1;
    visibility: visible;
  }

  .nav-toggle {
    display: block;
  }

  .auth-menu {
    flex-direction: column;
    width: 100%;
    gap: 1rem;
  }
}
</style>
